package com.xbh.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.token.Token;
import com.xbh.cache.MyTokenCache;
import com.xbh.dms.utils.GetControllerToken;
import com.xbh.zzzTest.RetTest;

/**
 * @MyActionInterceptor: Action拦截器
 * @author: ZBoHang
 * @time: 2021/8/5 8:58
 */
public class MyActionInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation invocation) {

        Controller controller = invocation.getController();
        if (controller != null) {
            if (controller.toString().contains("Login")) {
                invocation.invoke();
                return;
            }
            String token = GetControllerToken.getToken(controller);
            if (StrKit.notBlank(token)) {
                // 先验证token 验证成功后后调用
                boolean isExistToken = MyTokenCache.me.contains(new Token(token, 0));
                if (isExistToken) {
                    invocation.invoke();
                    return;
                }
                controller.renderJson(RetTest.me.errorAuth());
                return;
            }
            controller.renderJson(RetTest.me.nullAuth());
            return;
        }
        controller.renderJson(RetTest.me.nullControllerPath());
    }
}
